<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const data = [ 
                { id: '1000', name: '深圳', children: [ { id: '1001', name: '宝安', children: [] }, { id: '1002', name: '南山', children: [ { id: '1012', name: '粤海街道', children: [ { id: '1112', name: '阿里中心', children: [] }, { id: '1212', name: '深圳湾人才公园', children: [] } ] } ] } ] }, { id: '2000', name: '广州', children: [ { id: '2001', name: '越秀区', children: [ { id: '2011', name: '人民公园' } ] }, { id: '2002', name: '天河区', children: [] } ] } ];
        
        let a
        function find(arr, target) {
            // if(arr.some(item => item.id == target)) {
            //     a = arr.find(item => item.id == target).name
            // }else{

            //     arr.forEach(item => {
            //         if(item.children && item.children.length) {
            //                 return find(item.children, target)
            //         }
            //     }) 
            // }
            // return a  

            if(arr.some(i => i.id == target)) {
                console.log("找到了",arr.some(i => i.id == target),  arr.find(i => i.id == target).name)
                a= arr.find(i => i.id == target).name
            } else {
                arr.forEach(item => {
                    if(item.children && item.children.length) {
                        console.log("children 的长度:", item.children.length)
                        find(item.children, target)
                    }
                })
            }

            return a
        }

        

        // console.log( find(data, '1000')) // => '深圳'
        console.log(find(data, '2001')) // => '越秀区'
        // console.log(find(data, '1012')) // => '粤海街道'
        // console.log(find(data, '1112')) // => '阿里中心'
            // find(data, '1222') // => '未找到'
    </script>
</body>
</html>